package utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;

/**
 * A program that demonstrates how to upload files from local computer to a
 * remote FTP server using Apache Commons Net API.
 * 
 * @author www.codejava.net
 */
public class FTPUploadFile {
	static String response = null;

	public FTPUploadFile() {
	}

	public String UploadImage(String filePath, String addressUpload,
			String fileName) {
		String server = "www.shotandsell.com";
		int port = 21;
		String user = "quangnm92";
		String pass = "admin123";
		FTPClient ftpClient = new FTPClient();
		try {
			ftpClient.connect(server, port);
			ftpClient.login(user, pass);
			ftpClient.enterLocalPassiveMode();
			ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
			// APPROACH #1: uploads first file using an InputStream
			File localFile = new File(filePath);
			String remoteFile = "/img.shotandsell.com/wwwroot/img/"
					+ addressUpload + fileName;
			response = "http://www.img.shotandsell.com/img/" + addressUpload
					+ fileName;
			InputStream inputStream = new FileInputStream(localFile);
			// System.out.println("Start uploading first file");
			@SuppressWarnings("unused")
			boolean done = ftpClient.storeFile(remoteFile, inputStream);
			inputStream.close();
		} catch (IOException ex) {
			System.out.println("Error: " + ex.getMessage());
			ex.printStackTrace();
		} finally {
			try {
				if (ftpClient.isConnected()) {
					ftpClient.logout();
					ftpClient.disconnect();
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		return response;
	}
}